//
//  ChargingCaseViewController.swift
//  EasePods
//
//  Created by 易先生 on 2024/4/7.
//

import UIKit
import RxCocoa
import RxSwift

class ChargingCaseViewController: ViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    override func makeUI() {
        super.makeUI()
        
        self.view.backgroundColor = R.color.common_10_bg_page()
        
        self.view.addSubview(headView)
        self.view.addSubview(tableView)
        
        self.headView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(kNavHeight)
            make.leading.trailing.equalToSuperview()
            make.height.greaterThanOrEqualTo(40)
        }
    }
    
    override func bindViewModel() {
        super.bindViewModel()
        BluetoothManager.shared.connectState.subscribe { [weak self] state in
            guard let self = self else { return }
                        
            self.tableView.snp.makeConstraints { make in
                if state == .connected {
                    make.top.equalToSuperview().offset(kNavHeight)
                    self.headView.isHidden = true
                    self.tableView.isUserInteractionEnabled = true
                } else {
                    make.top.equalTo(self.headView.snp.bottom)
                    self.headView.isHidden = false
                    self.tableView.isUserInteractionEnabled = false
                }
                make.leading.trailing.bottom.equalToSuperview()
            }
        }.disposed(by: rx.disposeBag)
        
        viewModel.getChargingCaseItems()
        viewModel.itemsRelay.bind(to: tableView.rx.items(cellIdentifier: ChargingCaseTableViewCell.reuseIdentifier, cellType: ChargingCaseTableViewCell.self)){ i, item, cell in
            cell.bind(to: item)
        }.disposed(by: rx.disposeBag)
        
        tableView.rx.itemSelected.subscribe(onNext: { [weak self] indexPath in
            guard let self = self else { return }
            
            let cellModel = self.viewModel.itemsRelay.value[indexPath.row]
            switch cellModel.cellType {
            case .timeFormat:
                pickerPopView.bind(to: self.viewModel.timeFormatDatas,
                                   title: R.string.localizable.setting_time_format_text(),
                                   selectIndex: self.viewModel.timeFormatIndex)
                showPopupView(cellType: cellModel.cellType, indexPath: indexPath)
            case .earCaseLanguage:
                pickerPopView.bind(to: self.viewModel.languageDatas,
                                   title: R.string.localizable.setting_ear_case_language_text(),
                                   selectIndex: self.viewModel.currentLanguageIndex)
                showPopupView(cellType: cellModel.cellType, indexPath: indexPath)
            case .lockScreen:
                
                let datas = self.viewModel.lockScreenDatas.map { time in
                    return "\(time)" + R.string.localizable.common_second()
                }
                pickerPopView.bind(to: datas,
                                   title: R.string.localizable.setting_automatic_lock_screen_text(),
                                   selectIndex: self.viewModel.autoLockTimeIndex)
                showPopupView(cellType: cellModel.cellType, indexPath: indexPath)
            case .notification:
                let vc = NotificationViewController()
                self.navigationController?.pushViewController(vc, animated: true)
            case .contacts:
                
                let vc = AddressBookController()
                self.navigationController?.pushViewController(vc, animated: true)
                
            default:
                break
            }
            
        }).disposed(by: rx.disposeBag)
        
    }
    
    private func showPopupView(cellType: ChargingCasecellType, indexPath: IndexPath) {

        self.pickerPopView.confirmBlock = { [weak self] selectIndex in
            guard let self = self else { return }
            UIView.dismissSheet()
            switch cellType {
            case .timeFormat:
                guard selectIndex<self.viewModel.timeFormatDatas.count else {
                    return
                }
                self.viewModel.updateTimeFormat(selectIndex: selectIndex, indexPath: indexPath)
            case .earCaseLanguage:
                guard selectIndex<self.viewModel.languageDatas.count else {
                    return
                }
                self.viewModel.updateEarCaseLanguage(selectIndex: selectIndex, indexPath: indexPath)
            case .lockScreen:
                guard selectIndex<self.viewModel.lockScreenDatas.count else {
                    return
                }
                self.viewModel.updateLockScreenTime(selectIndex: selectIndex, indexPath: indexPath)
            default:
                break
            }
        }
        UIView.showSheet(with: self.pickerPopView)
    }
    
    private lazy var pickerPopView: CustomPickerPopView = {
        let view = CustomPickerPopView()
        return view
    }()
    
    private lazy var viewModel: ChargingCaseViewModel = {
        let viewModel = ChargingCaseViewModel()
        return viewModel
    }()
    
    private lazy var headView: UnBoundTopTipsView = {
        let view = UnBoundTopTipsView()
        return view
    }()
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.backgroundColor = .clear
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        tableView.register(ChargingCaseTableViewCell.self, forCellReuseIdentifier: ChargingCaseTableViewCell.reuseIdentifier)
        tableView.estimatedRowHeight = 70
        tableView.estimatedSectionHeaderHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.rowHeight = UITableView.automaticDimension
        tableView.separatorColor = R.color.common_6_line()
        return tableView
    }()

}
